require 'time'

class Movie < ActiveRecord::Base
	@@ageLimits = { "G" => 0, "PG" => 0, "PG-13" => 13, "R" => 17, "NC-17" => 17 }

	validates_presence_of :title
	validates_uniqueness_of :title, :scope => :title
	validates_presence_of :description
	validates_length_of :description, :minimum => 10
	validates_inclusion_of :rating, :in => @@ageLimits.keys
	
	def appropriate_for_birthdate?(bday)
		ageLimit = @@ageLimits[self.rating]
		ageLimit == 0 || bday <= ageLimit.years.ago
	end

	def self.find_all_appropriate_for_birthdate(birthdate)
		whereClause = []

		@@ageLimits.each do |rating, ageLimit|
				whereClause << "rating = '#{rating}'" if birthdate <= ageLimit.years.ago
		end

		find(:all, :conditions => whereClause.join(" OR ") )
	end
end
